package virtorina.server;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Random;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheManager;

import virtorina.server.domain.Room;


//Singleton to all roooms 
public class Base {
	private Base() {

	}
	private static LinkedHashMap<Integer, Room> Rooms;
	public static Cache Stuff;
	private static Integer id = 1;
	private static void init() {
		Rooms = new LinkedHashMap<Integer, Room>();
		try {
			Stuff = CacheManager.getInstance().getCacheFactory()
					.createCache(Collections.emptyMap());
		} catch (CacheException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Stuff.put("anonymousID", new Long(-1));
		LinkedList<Integer> list = new LinkedList<Integer>();
		for (int i = 0; i < 4; i++) {
			list.add(i);
		}
		Rooms.put(0, new Room(0, "Common", new LinkedHashSet<Long>(), list));
	}
	
	

	public static Room getRoom(int id) {
		return Rooms.get(id);
	}

	public static boolean chkName(String name) {
		for (Room r : Rooms.values()) {
			if (r.getName().equals(name)) {
				return false;
			}
		}
		return true;
	}


	public static String getName(Integer ID) {
		if (!Rooms.containsKey(ID)) {
			return "@@@";
		} else {
			return Rooms.get(ID).getName();
		}
	}

	public static Collection<Room> getRooms() {
		if (Rooms == null){
			init();
		}
		return Rooms.values();
	}

	public static Integer addRoom(String name, LinkedHashSet<Long> lbls,
			LinkedList<Integer> types) {
		Integer id = Base.id++;
		Rooms.put(id, new Room(id, name, lbls, types));
		return id;

	}

}
